{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1 安装Shenma自定义库\n",
    "\n",
    "一个用于“神码童学”人工智能教学的自定义库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.doubanio.com/simple\n",
      "Requirement already satisfied: Shenma in c:\\programdata\\anaconda3\\lib\\site-packages (0.1.9)\n",
      "Requirement already satisfied: PyAudio>=0.2.11 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (0.2.11)\n",
      "Requirement already satisfied: playsound>=1.2.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (1.2.2)\n",
      "Requirement already satisfied: matplotlib>=2.1.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (2.1.2)\n",
      "Requirement already satisfied: baidu-aip>=2.2.18.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (2.2.18.0)\n",
      "Requirement already satisfied: requests>=2.23.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (2.23.0)\n",
      "Requirement already satisfied: numpy>=1.14.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from Shenma) (1.14.0)\n",
      "Requirement already satisfied: six>=1.10 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib>=2.1.2->Shenma) (1.11.0)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib>=2.1.2->Shenma) (2.6.1)\n",
      "Requirement already satisfied: pytz in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib>=2.1.2->Shenma) (2017.3)\n",
      "Requirement already satisfied: cycler>=0.10 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib>=2.1.2->Shenma) (0.10.0)\n",
      "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib>=2.1.2->Shenma) (2.2.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.23.0->Shenma) (2018.4.16)\n",
      "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.23.0->Shenma) (1.22)\n",
      "Requirement already satisfied: chardet<4,>=3.0.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.23.0->Shenma) (3.0.4)\n",
      "Requirement already satisfied: idna<3,>=2.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests>=2.23.0->Shenma) (2.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install Shenma"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 导入自定义库\n",
    "Shenma库为用于人工智能教学的自定义库，其中\n",
    "- Shenma.sound中包含了可以实现声音录制、播放、可视化等操作的方法\n",
    "- Shenma.speech中包含了语音技术的相关方法\n",
    "- Shenma.translate中包含了语音翻译的相关方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Shenma.sound import *\n",
    "from Shenma.speech import *\n",
    "from Shenma.translate import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 配置filePath参数\n",
    "以‘16k.wav’为例，完成接下来的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "filePath = '16k.wav'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4 播放音频\n",
    "通过**play(filePath)**方法播放测试音频，了解音频的内容，用于和翻译结果做对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始播放\n",
      "该音频的时长为4秒\n",
      "播放结束\n"
     ]
    }
   ],
   "source": [
    "play(filePath)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5 语音翻译"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.1 配置client参数\n",
    "在下面的代码中，常量**APP_ID**在百度云控制台中创建，常量**API_KEY**与**SECRET_KEY**是在创建完毕应用后，系统分配给用户的，均为字符串，用于标识用户，为访问做签名验证，可在AI服务控制台中的应用列表中查看。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "APP_ID = '你的 App ID'\n",
    "API_KEY = '你的 Api Key'\n",
    "SECRET_KEY = '你的 Secret Key'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.2 创建Translation\n",
    "**Translation(filePath, APP_ID, API_KEY, SECRET_KEY)**是**Shenma.translate**中的语音翻译客户端，传入音频文件，可以获得语音转化成文字内容，显示语音内容的原文和译文\n",
    "\n",
    "- filePath：表示音频文件的路径；字符串数据类型\n",
    "\n",
    "以变量**client**实例化Translation客户端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = Translation(filePath, APP_ID, API_KEY, SECRET_KEY)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3 实现语音翻译\n",
    "利用**client.result()**方法，获得翻译的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原文：北京科技馆。\n",
      "译文：Beijing science and technology museum.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "client.result()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
